package com.shzu.jwc.ty.controller;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.shzu.jwc.ty.bean.SelectProblem;
import com.shzu.jwc.ty.service.Testservice;

@Controller
@RequestMapping("/jwc/test")
public class Testcontroller {

	@Autowired
	Testservice testservice;
	@RequestMapping("/test1.action")
	public void test(HttpServletResponse response) throws IOException
	{
		PrintWriter out=response.getWriter();
		out.println(1111);
	}
	
	@ResponseBody
	@RequestMapping("/hello.action")
	public String hello()
	{
		return "hello";
	}
	
	@RequestMapping("/testupload1.action")
	public void testupload1(@RequestParam("file") MultipartFile file,HttpSession session,HttpServletResponse response) throws IOException
	{
		String url=session.getServletContext().getRealPath("upload");
		System.out.println(url);
		String filename=file.getOriginalFilename();
		File targetFile = new File(url, filename);  
		file.transferTo(targetFile);
	        PrintWriter out=response.getWriter();  
	    	out.println(1111);

	}
	@RequestMapping("/testupload.action")
	public void testupload(@RequestParam("file") MultipartFile file,HttpServletResponse response) throws IOException
	{
		List<String> list=new ArrayList<String>();
		SelectProblem selectProblem=new SelectProblem();
		String[] a=new String[10];
		BufferedReader br=new BufferedReader(new InputStreamReader(file.getInputStream(),"GBK"));
		String stemp;
		br.readLine();
		while((stemp = br.readLine()) != null)
		{
			list.add(stemp);
		}
		a=list.get(0).split(",");
		for(Integer i=0;i<list.size();i++) {
			a=list.get(i).split(",");
			selectProblem.setSelectProblemTitle(a[0]);
			selectProblem.setSelectProblemOption1(a[1]);
			selectProblem.setSelectProblemOption2(a[2]);
			selectProblem.setSelectProblemOption3(a[3]);
			selectProblem.setSelectProblemOption4(a[4]);
			selectProblem.setSelectProblemRightoption(a[5]);
			selectProblem.setProblemClassId(1);
			selectProblem.setSelectProblemStatue(0);
			testservice.insertselect(selectProblem);
		}

	        PrintWriter out=response.getWriter();  
	    	out.println(1111);

	}
}
